<?php

namespace App\Http\Controllers\Home;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\MySqlConnection;
use Illuminate\Support\Facades\Session;
use Redirect;

class LoginController extends BaseController
{
    //注册
   public function login(Request $request){
       if($request->isMethod('POST')){
           $info=$request->input();
           $user_name=$info['user_name'];
           $user_pwd=isset($info['user_pwd'])?$info['user_pwd']:000000;

           $user_pwd=md5($user_pwd);
           $email=$info['email'];
               $res=DB::insert("insert into wfl_user (user_name,user_pwd,email) VALUE (?,?,?)",[$user_name,$user_pwd,$email]);
               $id=DB::table('wfl_user')->where('user_name',$user_name)->first();
               $id=$id->user_id;
               if($res){
                   Session::put('user_id',$id);
                   Session::put('user',$user_name);
                   return Redirect::to('home/user/info');
               }

       }else{
           return view('home/login/login');
       }
   }
    //登陆
    public function login_two(Request $request){
        $login=$request->input();
        $user_name=$login['user_name'];
        $user_pwd=$login['user_pwd'];
        $user_pwd=md5($user_pwd);
        $onec=DB::table('wfl_user')->where("user_name",$user_name)->where("user_pwd",$user_pwd)->first();
//        print_r($onec);die;
       if($onec){
           Session::put('user',$user_name);
           return Redirect::to('home/index/index');
       }else{
           echo "<script>alert('用户名或密码错误!!!');location.href='login'</script>";
       }
    }
    //验证用户名唯一
    public function ajax(){
        $user_name=$_GET['user_name'];
        $yan=DB::table('wfl_user')->where('user_name',$user_name)->first();
        if($yan){
            echo 1;
        }else{
            echo 0;
        }
    }
}


